#include "HttpIPUpdate.h"

#include "poco/URIStreamOpener.h"
#include "poco/uri.h"
#include "poco/exception.h"
#include "poco/net/httpstreamfactory.h"

#include <istream>
#include <string>
#include <stdexcept>
#include <memory>

using Poco::URIStreamOpener;
using Poco::Exception;
using Poco::URI;
using Poco::Net::HTTPStreamFactory;

using std::string;
using std::istream;
using std::auto_ptr;
using std::runtime_error;

HttpIPUpdate::HttpIPUpdate(const std::string& url)
:m_Url(url)
{
	try{
		HTTPStreamFactory::registerFactory();
	}catch(const Exception&)
	{
	}
	
}

void HttpIPUpdate::updateIP(const std::string& newIP)
{
	throw std::exception("Http do not support update ip operation");
}

std::string HttpIPUpdate::getIP() const
{
	try{
		URI uri(m_Url);
		auto_ptr<istream> ipStream ( URIStreamOpener::defaultOpener().open(uri) );

		string ip;
		(*ipStream) >> ip;

		return ip;
	}catch(const Exception& e)
	{
		throw runtime_error(e.displayText());
	}

	return string();

}